//
//  ListViewController.swift
//  DeckTracker
//
//  Created by YJH on 2025/5/5.
//

import UIKit
import SnapKit

// 定义列表项结构体
struct ListItem {
    let number: Int
    let text: String
    let color: UIColor
    let imageName: String
}

// 定义列表数据模型结构体
struct ListModel {
    let items: [ListItem]
    
    init() {
        self.items = [
            .init(number: 1, text: "唤醒造物者", color: .brown, imageName: "card_akama"),
            .init(number: 1, text: "亡者之灵", color: .blue, imageName: "card_luokehuola"),
            .init(number: 2, text: "战利品贮藏者", color: .white, imageName: ""),
            .init(number: 2, text: "战利品贮藏者", color: .white, imageName: ""),
            .init(number: 2, text: "血法师萨转", color: .brown, imageName: ""),
            .init(number: 2, text: "暗言术：痛", color: .white, imageName: ""),
            .init(number: 2, text: "暗言术：痛", color: .white, imageName: ""),
            .init(number: 2, text: "暗影视界", color: .purple, imageName: ""),
            .init(number: 2, text: "暗影视界", color: .purple, imageName: ""),
            .init(number: 2, text: "灵魂鞭笞", color: .white, imageName: ""),
            .init(number: 2, text: "灵魂鞭笞", color: .white, imageName: ""),
            .init(number: 2, text: "恶毒的银行家", color: .purple, imageName: ""),
            .init(number: 2, text: "恶毒的银行家", color: .purple, imageName: ""),
            .init(number: 2, text: "丧钟机器人", color: .white, imageName: ""),
            .init(number: 2, text: "丧钟机器人", color: .white, imageName: "")
        ]
    }
}

class ListViewController: UIViewController {
    
    let data = ListModel()
    
    // 懒加载 tableView
    lazy var tableView: UITableView = {
        let tableView = UITableView(frame: .zero, style: .plain)
        tableView.register(ListCell.self, forCellReuseIdentifier: "cell")
        tableView.rowHeight = 44
        tableView.backgroundColor = .black
        tableView.dataSource = self
        tableView.delegate = self
        return tableView
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupViews()
        
        let titleLabel = UILabel()
        titleLabel.text = "炉石传说记牌器"
        titleLabel.textColor = .white
        titleLabel.font = .systemFont(ofSize: 16, weight: .medium)
        navigationItem.titleView = titleLabel
    }
    
    // 设置视图布局
    private func setupViews() {
        view.addSubview(tableView)
        tableView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
    }
}

// MARK: - table view data source
// 实现 UITableViewDataSource 和 UITableViewDelegate 协议方法
extension ListViewController: UITableViewDataSource, UITableViewDelegate {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        data.items.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        guard let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as? ListCell
        else { return .init() }
        let item = data.items[indexPath.row]
        cell.numberLabel.text = String(item.number)
        cell.nameLabel.text = item.text
        cell.nameLabel.textColor = item.color
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let model = data.items[indexPath.row]
        print(model.text)
        let detailVC = DetailViewController()
        // 传递数据到 DetailViewController
        detailVC.selectedListItem = model
        navigationController?.pushViewController(detailVC, animated: true)
    }
}

class ListCell: UITableViewCell {
    
    // 懒加载数字标签
    lazy var numberLabel: UILabel = {
        let label = UILabel()
        label.textColor = .white
        label.font = .systemFont(ofSize: 16, weight: .medium)
        label.textAlignment = .left
        return label
    }()
    
    // 懒加载名称标签
    lazy var nameLabel: UILabel = {
        let label = UILabel()
        label.textColor = .darkText
        label.font = .systemFont(ofSize: 14, weight: .medium)
        return label
    }()
    
    // 懒加载水晶图片视图
    lazy var crystalImageView: UIImageView = {
        let imageView = UIImageView()
        imageView.image = UIImage(named: "energy")
        return imageView
    }()
    
    // 懒加载图标图片视图
    lazy var iconImageView: UIImageView = {
        let imageView = UIImageView()
        return imageView
    }()
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupViews()
        contentView.backgroundColor = .gray
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // 设置单元格内视图布局
    private func setupViews() {
        contentView.addSubview(crystalImageView)
        crystalImageView.snp.makeConstraints { make in
            make.left.equalToSuperview()
            make.centerY.equalToSuperview()
            make.width.height.equalTo(contentView.snp.height)
        }
        
        contentView.addSubview(numberLabel)
        numberLabel.snp.makeConstraints { make in
            make.center.equalTo(crystalImageView)
        }
        
        contentView.addSubview(nameLabel)
        nameLabel.snp.makeConstraints { make in
            make.left.equalTo(crystalImageView.snp.right).offset(20)
            make.centerY.equalToSuperview()
        }
    }
}
